package com.jack.blog.controller;

import com.jack.blog.cache.*;
import com.jack.blog.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private BlogServiceCache blogServiceCache;
    @Autowired
    private CategoryServiceCache categoryServiceCache;
    @Autowired
    private NoticeServiceCache noticeServiceCache;
    @Autowired
    private MessageServiceCache messageServiceCache;

    @GetMapping("/user/{uid}/blogs")
    public String blogs(@PathVariable("uid")int uid, Model model){
        try {
            UserVo user = userServiceCache.getUser(uid);
            List<BlogVo> blogs = blogServiceCache.getBlogs(uid, 0);
            model.addAttribute("user", user);
            model.addAttribute("blogs",blogs);
            return "blogs";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/addBlog")
    public String createBlog(@PathVariable("uid")int uid,Model model){
        try {
            List<CategoryVo> categories = categoryServiceCache.getCategories(uid);
            model.addAttribute("uid", uid);
            model.addAttribute("categories",categories);
            return "createBlog";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/updateBlog")
    public String updateBlog(@PathVariable("uid")int uid,@RequestParam("bid")int bid,Model model){
        try {
            BlogVo blog = blogServiceCache.getBlog(bid);
            List<CategoryVo> categories = categoryServiceCache.getCategories(uid);
            model.addAttribute("uid",uid);
            model.addAttribute("blog", blog);
            model.addAttribute("categories",categories);
            return "updateBlog";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/categories")
    public String categories(@PathVariable("uid")int uid,Model model){
        try {
            List<CategoryVo> categories = categoryServiceCache.getCategories(uid);
            model.addAttribute("uid", uid);
            model.addAttribute("defaultCategory",categories.get(0));
            model.addAttribute("categories",categories.subList(1, categories.size()));
            return "blogCategories";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/addCategory")
    public String createCategory(@PathVariable("uid")int uid,Model model){
        try {
            model.addAttribute("uid",uid);
            return "createBlogCategory";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/updateCategory")
    public String updateCategory(@PathVariable("uid")int uid, @RequestParam("cid") int cid, Model model){
        try {
            CategoryVo category = categoryServiceCache.getCategory(uid,cid);
            model.addAttribute("category", category);
            return "updateBlogCategory";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/messages")
    public String message(@PathVariable("uid")int uid,@RequestParam(name = "reply",defaultValue = "false",required = false)boolean reply,Model model){
        try {
            Map<BlogVo, List<MessageVo>> map = messageServiceCache.getMessagesByUid(uid, reply);
            model.addAttribute("uid",uid);
            model.addAttribute("map",map);
            return "message";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/notices")
    public String notice(@PathVariable("uid")int uid,Model model){
        try {
            List<NoticeVo> notices = noticeServiceCache.getNotices(uid);
            model.addAttribute("uid",uid);
            model.addAttribute("notices",notices);
            return "notice";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/addNotice")
    public String addNotice(@PathVariable("uid")int uid,Model model){
        try {
            model.addAttribute("uid",uid);
            return "createNotice";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}")
    public String profile(@PathVariable("uid")int uid, Model model){
        try {
            UserVo user = userServiceCache.getUser(uid);
            model.addAttribute("user", user);
            return "profile";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/updateUser")
    public String updateProfile(@PathVariable("uid")int uid,Model model){
        try {
            UserVo user = userServiceCache.getUser(uid);
            model.addAttribute("user", user);
            return "updateUser";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }
}
